package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.domain.req.BcpDeviceParamReq;
import com.ruibang.glass.produce.domain.resp.*;
import com.ruibang.glass.produce.mapper.BcpDeviceParamMapper;
import com.ruibang.glass.produce.service.BcpDeviceParamService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-04-19  10:20
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class BcpDeviceParamServiceImpl implements BcpDeviceParamService {

    @Autowired
    private BcpDeviceParamMapper bcpDeviceParamMapper;

    @Override
    public IPage<HqDeviceParamInfo> queryHqDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq) {
        //默认时间
        setDefaultTime(bcpDeviceParamReq);
        //查询
        return bcpDeviceParamMapper.queryHqDeviceParamInfo(new Page<>(bcpDeviceParamReq.getPageNo(), bcpDeviceParamReq.getPageSize()), bcpDeviceParamReq);
    }

    @Override
    public IPage<ZqDeviceParamInfo> queryZqDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq) {
        //默认时间
        setDefaultTime(bcpDeviceParamReq);
        //查询
        return bcpDeviceParamMapper.queryZqDeviceParamInfo(new Page<>(bcpDeviceParamReq.getPageNo(), bcpDeviceParamReq.getPageSize()), bcpDeviceParamReq);
    }

    @Override
    public IPage<BbDeviceParamInfo> queryBbDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq) {
        //默认时间
        setDefaultTime(bcpDeviceParamReq);
        //查询
        return bcpDeviceParamMapper.queryBbDeviceParamInfo(new Page<>(bcpDeviceParamReq.getPageNo(), bcpDeviceParamReq.getPageSize()), bcpDeviceParamReq);
    }

    @Override
    public IPage<SbDeviceParamInfo> querySbDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq) {
        //默认时间
        setDefaultTime(bcpDeviceParamReq);
        //查询
        return bcpDeviceParamMapper.querySbDeviceParamInfo(new Page<>(bcpDeviceParamReq.getPageNo(), bcpDeviceParamReq.getPageSize()), bcpDeviceParamReq);
    }

    @Override
    public IPage<XbDeviceParamInfo> queryXbDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq) {
        //默认时间
        setDefaultTime(bcpDeviceParamReq);
        //查询
        return bcpDeviceParamMapper.queryXbDeviceParamInfo(new Page<>(bcpDeviceParamReq.getPageNo(), bcpDeviceParamReq.getPageSize()), bcpDeviceParamReq);
    }

    @Override
    public IPage<FxDeviceParamInfo> queryFxDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq) {
        //默认时间
        setDefaultTime(bcpDeviceParamReq);
        //查询
        return bcpDeviceParamMapper.queryFxDeviceParamInfo(new Page<>(bcpDeviceParamReq.getPageNo(), bcpDeviceParamReq.getPageSize()), bcpDeviceParamReq);
    }

    @Override
    public void exportHqDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq, HttpServletResponse response) {
        bcpDeviceParamReq.setPageNo(-1);
        bcpDeviceParamReq.setPageSize(-1);
        IPage<HqDeviceParamInfo> page = this.queryHqDeviceParamInfo(bcpDeviceParamReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<HqDeviceParamInfo> hqDeviceParamInfoList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), HqDeviceParamInfo.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("横切设备参数").doWrite(hqDeviceParamInfoList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportZqDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq, HttpServletResponse response) {
        bcpDeviceParamReq.setPageNo(-1);
        bcpDeviceParamReq.setPageSize(-1);
        IPage<ZqDeviceParamInfo> page = this.queryZqDeviceParamInfo(bcpDeviceParamReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<ZqDeviceParamInfo> zqDeviceParamInfoList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), ZqDeviceParamInfo.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("纵切设备参数").doWrite(zqDeviceParamInfoList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportBbDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq, HttpServletResponse response) {
        bcpDeviceParamReq.setPageNo(-1);
        bcpDeviceParamReq.setPageSize(-1);
        IPage<BbDeviceParamInfo> page = this.queryBbDeviceParamInfo(bcpDeviceParamReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<BbDeviceParamInfo> bbDeviceParamInfos = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), BbDeviceParamInfo.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("掰板设备参数").doWrite(bbDeviceParamInfos);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportSbDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq, HttpServletResponse response) {
        bcpDeviceParamReq.setPageNo(-1);
        bcpDeviceParamReq.setPageSize(-1);
        IPage<SbDeviceParamInfo> page = this.querySbDeviceParamInfo(bcpDeviceParamReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<SbDeviceParamInfo> sbDeviceParamInfoList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), SbDeviceParamInfo.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("上板设备参数").doWrite(sbDeviceParamInfoList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportXbDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq, HttpServletResponse response) {
        bcpDeviceParamReq.setPageNo(-1);
        bcpDeviceParamReq.setPageSize(-1);
        IPage<XbDeviceParamInfo> page = this.queryXbDeviceParamInfo(bcpDeviceParamReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<XbDeviceParamInfo> xbDeviceParamInfoList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), XbDeviceParamInfo.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("下板设备参数").doWrite(xbDeviceParamInfoList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportFxDeviceParamInfo(BcpDeviceParamReq bcpDeviceParamReq, HttpServletResponse response) {
        bcpDeviceParamReq.setPageNo(-1);
        bcpDeviceParamReq.setPageSize(-1);
        IPage<FxDeviceParamInfo> page = this.queryFxDeviceParamInfo(bcpDeviceParamReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<FxDeviceParamInfo> fxDeviceParamInfoList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), FxDeviceParamInfo.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("分选设备参数").doWrite(fxDeviceParamInfoList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    public void setDefaultTime(BcpDeviceParamReq bcpDeviceParamReq) {
        if (bcpDeviceParamReq.getStartTime() == null || bcpDeviceParamReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(3);
            bcpDeviceParamReq.setStartTime(startTime);
            bcpDeviceParamReq.setEndTime(endTime);
            bcpDeviceParamReq.setStartTs(String.valueOf(startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()));
            bcpDeviceParamReq.setEndTs(String.valueOf(endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()));
        } else {
            bcpDeviceParamReq.setStartTs(String.valueOf(bcpDeviceParamReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()));
            bcpDeviceParamReq.setEndTs(String.valueOf(bcpDeviceParamReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()));
        }
    }
}
